<?php

/*
 * Generic function for easier array access
 */

function get_array_key($array, $key)
{
	if(isset($array[$key]))
		$value = $array[$key];
	else
		$value = null;
	return $value;
}

/*
 * 
 */

function cookie($key, $value=null, $time=null)
{
	if(is_null($time))
		$time = time()+3600;
	if(is_null($value))
	{
		$return = get_array_key($_COOKIE, $key);
		return $return;
	}
	else
	{
		setcookie($key, $value, $time);
	}
}
 
function session($key, $value=null)
{
	session_start();
	if(is_null($value))
	{
		$return = get_array_key($_SESSION, $key);
		session_write_close();
		return $return;
	}
	else
	{
		$_SESSION[$key] = $value;
		session_write_close();
	}
}

function post($key)
{
	return get_array_key($_POST, $key);
}

function get($key)
{
	return get_array_key($_GET, $key);
}

function server($key)
{
	return get_array_key($_SERVER, $key);
}

function request($key)
{
	return get_array_key($_REQUEST, $key);
}


/* making objects of a nested array (db result) */
function aa2oa($array)
{
	$array2 = array();
	foreach( $array as $element)
		$array2[] =(object)$element;
	return $array2;
}


/* Translation */
function language($langCode = false)
{
	if($langCode === false)
	{
		if(App::$Current->Language == '')
			return 'en';
		else
			return App::$Current->Language;
	}
	else
	{
		session('language', $langCode);
		App::$Current->Language = $langCode;
	}
}
function translate($key)
{
	return TranslationManager::GetText($key, language());
}
function text($key, $text = false, $langCode = false)
{
	if($text === false)
		print TranslationManager::GetText($key, language());
	else 
		if($langCode === false)
			TranslationManager::SetText($key, language(), $text);
		else
			TranslationManager::SetText($key, $langCode, $text);
}